package com.kwan.x_im.base;

import android.os.Bundle;


import com.jeremyliao.liveeventbus.LiveEventBus;
import com.kwan.x_im.data.IMViewMode;
import com.kwan.x_im.data.http.HttpEvent;
import com.kwan.xframe.mvvm.view.fragment.BaseFragment;

import org.json.JSONException;

import androidx.annotation.Nullable;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.Observer;
import timber.log.Timber;

/**
 * @author kwan
 * @date 2023/2/20 10:26
 * @Describe
 */
public abstract class IMBaseFragment<V extends ViewDataBinding> extends BaseFragment<V> {

	public IMViewMode mIMViewMode;

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mIMViewMode = getActivityScopeViewModel(IMViewMode.class);
		getLifecycle().addObserver(mIMViewMode);


		LiveEventBus.get(HttpEvent.class)
				.observe(this, new Observer<HttpEvent>() {
					@Override
					public void onChanged(HttpEvent httpEvent) {
						Timber.e("onHttpResult:" + httpEvent.func + " Msg:" + httpEvent.getMsg());
						try {
							onHttpResult(httpEvent);
						} catch (JSONException e) {
							throw new RuntimeException(e);
						}
					}
				});
	}



	public abstract void onHttpResult(HttpEvent<?> httpEvent) throws JSONException;
}
